package com.wan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wan.common.CustomException;
import com.wan.mapper.CategoryMapper;
import com.wan.pojo.Category;
import com.wan.pojo.Dish;
import com.wan.pojo.Setmeal;
import com.wan.service.CategoryService;
import com.wan.service.DishService;
import com.wan.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setMealService;

    /**
     * 根据id删除菜品 删除前需要判断
     * @param id
     * @return
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int countDish = dishService.count(dishLambdaQueryWrapper);
        // 判断有没有关联菜品 如果关联，不允许删除，抛出业务异常
        if (countDish > 0){
            //已经关联，不允许删除，抛出业务异常
            throw new CustomException("当前分类关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int countSetMeal = setMealService.count(setmealLambdaQueryWrapper);
        // 判断有没有关联套餐 如果关联，不允许删除，抛出业务异常
        if (countSetMeal > 0){
            //已经关联，不允许删除，抛出业务异常
            throw new CustomException("当前分类关联了套餐，不能删除");
        }
        // 正常删除分类
        super.removeById(id);
    }
}
